import FormCodegenBusinessEdit3Panel from "@/views/modules/form/codegen/businessEdit/FormCodegenBusinessEdit3Panel";
import {Util} from "@lowcode/jpaas-common-lib";

export default {
    props:["formData"],
    components:{
        FormCodegenBusinessEdit3Panel
    },
    data(){
        return {
            panelActiveKeys:['main'],
            settingJson:{},
            formConfig:{
                //主表配置
                main:{},
                //子表配置
                sub:[]
            }
        }
    },
    created(){
        this.init();
    },
    methods:{
        i18n(name,text,key){
            return this.$ti18(name,text,"formCodegenBusiness",key);
        },
        init() {
            this.settingJson = JSON.parse(this.formData.settingJson);
            let tmpVars = this.settingJson.tmpVars;
            let mainEntity;
            //数据来源：业务模型
            if (this.formData.source == "businessModel") {
                mainEntity=tmpVars["mainEntity"];
            }
            if (this.settingJson.hasOwnProperty("formConfig")) {
                let formConfig = Util.deepClone(this.settingJson.formConfig);
                if (mainEntity) {
                    let main = this.getEntityJson(mainEntity);
                    delete main.className;
                    delete main.package;
                    delete main.packageName;
                    Object.assign(formConfig.main, main);
                    let subAttrListObj = {};
                    mainEntity.boEntityList.forEach(item => {
                        subAttrListObj[item.name] = item;
                    })
                    formConfig.sub.forEach(item => {
                        let sub = this.getEntityJson(subAttrListObj[item.name]);
                        delete sub.className;
                        delete sub.package;
                        delete sub.packageName;
                        Object.assign(item, sub);
                    })
                }
                this.formConfig = formConfig;
            } else {
                this.formConfig.main = this.getEntityJson(mainEntity);
                this.formConfig.sub = mainEntity.boEntityList.map(item => {
                    return this.getEntityJson(item);
                })
            }
        },
        toHump(val,flag) {
            let result='';
            if(val) {
                val = val.toLowerCase();
                const arr = val.split('_')
                result=arr.map((ele,idx)=> {
                    if(!ele){
                        return;
                    }
                    return idx === 0 ? flag?ele.toUpperCase():ele.toLowerCase() : ele[0].toUpperCase() + ele.slice(1)
                }).join('');
            }
            return result;
        },
        getEntityJson(entity){
            let entityJson={
                name:entity.name,
                genMode:entity.genMode,
                className:this.toHump(entity.tableName,true),
                package:"core",
                packageName:"核心模块",
                tableName:entity.tableName,
                relationType:entity.boRelation.type,
                fkField:entity.boRelation.fkField,
                idField:entity.idField,
                dsAlias:entity.dsAlias,
                attrList:entity.boAttrList.map(item=>{
                    item.name=this.toHump(item.name,false);
                    return item;
                })
            }
            return entityJson;
        }
    }
}